package com.km.team.phsystem.service.impl;

import com.km.team.phsystem.beans.pojo.*;
import com.km.team.phsystem.dao.mybatis.BusinessStartEndInfoMapper;
import com.km.team.phsystem.dao.mybatis.SystemCommonConfMapper;
import com.km.team.phsystem.service.BusinessStartEndInfoService;
import com.km.team.phsystem.service.ISysBusiTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author ：bastr.
 * @description：设置每个月对应业务的开始和结束时间
 * @date ：2018/10/20
 */
@Service("businessStartEndInfoService")
public class BusinessStartEndInfoServiceImp implements BusinessStartEndInfoService {


    private final BusinessStartEndInfoMapper businessStartEndInfoMapper;
    private final ISysBusiTypeService iSysBusiTypeService;

    @Autowired
    public BusinessStartEndInfoServiceImp(
        BusinessStartEndInfoMapper businessStartEndInfoMapper,
        ISysBusiTypeService iSysBusiTypeService) {
        this.iSysBusiTypeService = iSysBusiTypeService;
        this.businessStartEndInfoMapper = businessStartEndInfoMapper;
    }

    @Override
    public List<BusinessStartEndInfo> selectForType() {

        List<SystemBusitype> SystemBusitype = null;
        try {
            SystemBusitype = iSysBusiTypeService.getSysBusiTypeData();
        } catch (Exception e) {
            e.printStackTrace();
        }

        BusinessStartEndInfoExample businessStartEndInfoExample = new BusinessStartEndInfoExample();
        BusinessStartEndInfoExample.Criteria startEndInfoExampleCriteria = businessStartEndInfoExample.createCriteria();
        startEndInfoExampleCriteria.andIdIsNotNull();
        List<BusinessStartEndInfo> businessStartEndInfos = businessStartEndInfoMapper.selectByExample(businessStartEndInfoExample);
        Set<String> systemCommonConfsSET = new HashSet<>(20);
        Set<String> businessStartEndInfosSET = new HashSet<>(20);

        if (SystemBusitype.size() != businessStartEndInfos.size()) {
            if (SystemBusitype.size() > businessStartEndInfos.size()) {
            SystemBusitype.forEach(item -> systemCommonConfsSET.add(item.getBusitype()+"A-B,d-f~"+item.getId()));
                businessStartEndInfos.forEach(item -> businessStartEndInfosSET.add(item.getBusinessTypeName()+"A-B,d-f~"+item.getBusinessTypeId()));
                Set<String> result = new HashSet<>(systemCommonConfsSET);
                result.removeAll(businessStartEndInfosSET);
                result.forEach(item -> {
                    BusinessStartEndInfo businessStartEndInfo = new BusinessStartEndInfo();
                    businessStartEndInfo.setStartOrEnd("S");
                    businessStartEndInfo.setDayId("1");
                    String[] items = item.split("A-B,d-f~");
                    businessStartEndInfo.setBusinessType(items[0]);
                    businessStartEndInfo.setBusinessTypeName(items[0]);
                    businessStartEndInfo.setBusinessTypeId(items[1]);
                    businessStartEndInfoMapper.insert(businessStartEndInfo);
                });
            }else{
                SystemBusitype.forEach(item -> systemCommonConfsSET.add(String.valueOf(item.getId())));
                businessStartEndInfos.forEach(item -> businessStartEndInfosSET.add(item.getBusinessTypeId()));
                Set<String> result = new HashSet<>(businessStartEndInfosSET);
                result.removeAll(systemCommonConfsSET);
                result.forEach(item -> {
                    startEndInfoExampleCriteria.andBusinessTypeIdEqualTo(item);
                    businessStartEndInfoMapper.deleteByExample(businessStartEndInfoExample);
                });
            }
        }
        businessStartEndInfoExample.clear();
        return businessStartEndInfoMapper.selectByExample(businessStartEndInfoExample);
    }

    @Override
    public void updateById(List<BusinessStartEndInfo> params) {
        params.forEach(businessStartEndInfoMapper::updateByPrimaryKeySelective);
    }

    @Override
    public BusinessStartEndInfo getDataByBusiType(String busiType) throws Exception {
        if(StringUtils.isBlank(busiType)){
            return null;
        }
        BusinessStartEndInfoExample example = new BusinessStartEndInfoExample();
        example.or().andBusinessTypeIdEqualTo(busiType);
        List<BusinessStartEndInfo> list = businessStartEndInfoMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public BusinessStartEndInfo selectDataForKey(Integer key) {
        BusinessStartEndInfoExample example = new BusinessStartEndInfoExample();
        example.or().andBusinessTypeIdEqualTo(String.valueOf(key));
        List<BusinessStartEndInfo> list = this.businessStartEndInfoMapper.selectByExample(example);
        if (list.size() != 0) {
            return list.get(0);
        }
        return new BusinessStartEndInfo();
    }
}
